class MapByAlert extends DialogPanel{
    loadList = ["main"];
    ui : layaUI.main.MapByAlertUI;
    uiClass = layaUI.main.MapByAlertUI;
    constructor() {
        super();
    }

    private static _instance:MapByAlert;//单例
    /**
     * 获取 单例
     */
    public static getInstance():MapByAlert{
        if(MapByAlert._instance == null){
            MapByAlert._instance = new MapByAlert();
        }
        return MapByAlert._instance;
    }

    public id:number;//购买的地图ID

    public buyIdList:Array<number> = [];//购买计时id列表

    /**
     * 初始化
     */
    protected onFirst():void{
        this.ui.btnBy.on(Laya.Event.CLICK,this,this.onBy);
        //语言
        this.ui.btnBy.label = LangUtils.getText(35);
        LangUtils.setSkin(this.ui.img1);
    }

    /**
     * 添加到舞台
     */
    protected onShow():void{
        this.ui.btnBy.gray = false;
        this.ui.txtNum.innerHTML = LangUtils.getText(31).replace("[5]",(this.id+1)+"");
        var idArray:Array<number> = MapModel.getInstance().occupyIdList;
        var occupy:boolean = idArray.indexOf(this.id) != -1;
        this.ui.textArea1.innerHTML = LangUtils.getText(occupy ? 33 : 32);
        if(occupy){
            var str:string = Utils.getShortAddress(MapModel.getInstance().addressList[this.id]);
            this.ui.textArea2.innerHTML = LangUtils.getText(36).replace("[1]",str);
            this.ui.btnBy.gray = true;
        }else{
            this.ui.textArea2.innerHTML = LangUtils.getText(6).replace("[6]"," "+(this.id+1));
        }
    }

    /**
     * 从舞台移除
     */
    protected onClose():void{

    }

    private onBy():void{
        if(this.ui.btnBy.gray == true)return;
        if(!Web3Manager.defaultAccount || Web3Manager.defaultAccount == ""){
            Alert.getInstance().showMsg = LangUtils.getText(18);
            Alert.getInstance().open();
            MapByAlert.getInstance().close();
            return;
        }
        var idArray:Array<number> = MapModel.getInstance().occupyIdList;
        var ownIdList:Array<number> = MapModel.getInstance().ownIdList;
        //判断是否被自己占领
        if(ownIdList.indexOf(this.id) != -1){
            Alert.getInstance().showMsg = LangUtils.getText(13);
            Alert.getInstance().open();
            MapByAlert.getInstance().close();
            return;
        }

        //判断是否被他人占领
        if(idArray.indexOf(this.id) != -1){
            Alert.getInstance().showMsg = LangUtils.getText(14);
            Alert.getInstance().open();
            MapByAlert.getInstance().close();
            return;
        }

        //检测是否已经在购买队列里面
        if(this.buyIdList.indexOf(this.id) != -1){
            Alert.getInstance().showMsg = LangUtils.getText(24);
            Alert.getInstance().open();
            MapByAlert.getInstance().close();
            return;
        }

        MapModel.getInstance().reqBuyMapById(this.id);
        MapBox.oldID = this.id;
        MapByAlert.getInstance().close();
    }

    /**
     * 计时购买id
     */
    public addBuyId():void{
        if(this.buyIdList.indexOf(this.id) == -1){
            this.buyIdList.push(this.id);
            setTimeout(this.removeBuyId,10000)
        }
    }

    //移除计时id
    removeBuyId=()=>{
        this.buyIdList.shift();
    }
}